package com.waypolice.manager.controller;

import com.waypolice.common.pojo.TaotaoResult;
import com.waypolice.common.utils.PageUtils;
import com.waypolice.manager.service.PermissionsService;
import com.waypolice.manager.service.RoleResourcesService;
import com.waypolice.manager.service.RoleService;
import com.waypolice.pojo.UpmsRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色
 * @author wr
 *
 */
@Controller
@Api(value = "角色", description = "角色管理")
@RequestMapping(value="/pcwindow")
public class RoleAction {

	private static final Logger LOGGER = LoggerFactory.getLogger(RoleAction.class);


	@Autowired
	private RoleService roleService;
	@Autowired
	private RoleResourcesService roleResourceService;
	@Autowired
	private PermissionsService permissionsService;
	//private ResourcesService resourceService;

	/**
	 * 角色管理页面跳转
	 * @param map
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/roleManager")
	public String userManagerJump(ModelMap map, HttpServletResponse response,
								  HttpServletRequest request) {
		return "/roleManager/role";
	}
	/**
	 * 查询全部角色
	 * @param response
	 * @param request
	 * @return
	 */
	@ApiOperation(value = "查询全部角色")
	@RequestMapping(value = "/findAllRole")
	@ResponseBody
	public Object findAllRole(@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
							  @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
							  HttpServletResponse response, HttpServletRequest request) {
		List<UpmsRole> roles = roleService.findAll();
		Map<String, Object> result = new HashMap<>(2);
		//最终的list集合,分页操作
		List<UpmsRole> finalList = new ArrayList<UpmsRole>();
		PageUtils<UpmsRole> pageUtils = new PageUtils<UpmsRole>();
		finalList = pageUtils.pageList(roles,offset,limit);
		result.put("rows",finalList);
		result.put("total",roles.size());
		return result;
	}

	/**
	 * 添加角色
	 * @return user
	 */
	@ApiOperation(value = "添加角色")
	@RequestMapping(value = "/roleManagerSet")
	@ResponseBody
	public TaotaoResult roleManagerSet(HttpServletRequest request, HttpServletResponse response) {
		TaotaoResult result = new TaotaoResult();
		Integer flag = Integer.parseInt(request.getParameter("flag"));
		String roleName = request.getParameter("roleName");
		String roleDescription = request.getParameter("roleDescription");
		String resourcesCode = request.getParameter("resourcesCode");
		if(flag == 1){
			 String roleId = request.getParameter("roleId");
			 result =roleService.editRole( roleName,  roleDescription,  resourcesCode,  roleId);
		}else{
			 result = roleService.insertRole(roleName, roleDescription, resourcesCode);
		}
		return result;
	}
	/**
	 * 编辑角色
	 * @return user
	 */
	@ApiOperation(value = "编辑角色")
	@RequestMapping(value = "/editRole")
	public TaotaoResult editRole(HttpServletRequest request, HttpServletResponse response) {
		String roleName = request.getParameter("roleName");
		String roleId = request.getParameter("roleId");
		String roleDescription = request.getParameter("roleDescription");
		String resourcesCode = request.getParameter("resourcesCode");
		return roleService.editRole(roleName,roleDescription,resourcesCode,roleId);
	}
	/**
	 * 根据Id角色
	 * @return
	 */
	@ApiOperation(value = "根据Id角色")
	@RequestMapping(value = "/findRoleByRoId")
	@ResponseBody
	public Object findRoleByRoId(HttpServletRequest request,HttpServletResponse response) {
		String roId = request.getParameter("roId");
		UpmsRole role = roleService.findRoleByRoId(roId);
		String  resourStr = null;
		if("".equals(role)){
		}else{
			resourStr = roleResourceService.findResourceByRoId(roId);
		}
		Map<String, Object> result = new HashMap<>(2);
		result.put("Role",role);
		result.put("resourStr",resourStr);
		return result;
	}

}
